home *** CD-ROM | disk | FTP | other *** search
-
- {
-
- Basic owner-draw list box object
-
-
- 16th April 1992
-
-
- Rex K. Perkins, CIS 70651,1611
-
-
-
- }
-
-
-
- Unit UOwnerDraw;
-
-
- Interface
-
- Uses WinTypes, WinProcs, WObjectB; {Replace WObjectsB with WObjects if 'B' is not available}
-
-
- Type
-
- POwnerDrawListBox=^TOwnerDrawListBox;
- TOwnerDrawListBox=Object(TListBox)
-
- Constructor Init(AParent: PWindowsObject; AnId: Integer;
- X,Y,W,H:Integer);
-
- Procedure WMDrawItem(DrawStruct:PDrawItemStruct); Virtual;
- {The parent has received a wm_DrawItem message.
- Pass a non nil LParam to this procedure & let it do the rest}
-
- Procedure ChangeFocus(DrawStruct:PDrawItemStruct); Virtual;
- {Change the focus of the ListBox item}
-
- Procedure ChangeSelection(DrawStruct:PDrawItemStruct); Virtual;
- {The selection of item DrawStruct has changed}
-
- Procedure DrawBox(DrawStruct:PDrawItemStruct); Virtual;
- {Draw the item for the ListBox line DrawStruct^.ItemID in the
- box DrawStruct^.rcItem. The colors are already set}
-
- Procedure DrawItem(DrawStruct:PDrawItemStruct); Virtual;
- {Draw the actual item in DrawStruct. Just draw it, everything else is
- taken care of. Must cope with no item selected (item -1)}
-
- End;
-
-
-
-
- Implementation
-
- Uses Strings;
-
-
- Constructor TOwnerDrawListBox.Init(AParent: PWindowsObject; AnId: Integer;
- X,Y,W,H:Integer);
-
- Begin {Create the list box, set the OwnerDraw attribute and stop auto-sorting}
- TListBox.Init(AParent,AnID,X,Y,W,H);
- Attr.Style:=(Attr.Style OR lbs_OwnerDrawFixed) AND NOT lbs_Sort
- End;
-
-
-
-
-
-
-
-
- Procedure TOwnerDrawListBox.WMDrawItem(DrawStruct:PDrawItemStruct);
-
- {The parent has received a wm_DrawItem message.
- Pass a non nil LParam to this procedure & let it do the rest}
-
-
- Begin
- {Ok, what do we want to do? At this point we can interogate both
- ItemAction and ItemState. ItemAction holds the CHANGE in item
- state whereas ItemState holds the desired state AFTER the message.
-
- If we need to change the selection state, call ChangeSelection only
- as this needs to call DrawBox and, if required, ChangeFocus}
-
- If DrawStruct^.ItemAction AND oda_Select>0 Then
- ChangeSelection(DrawStruct) {The selection changed, redraw this line}
- Else
- Begin {No change of selection, just draw or change focus}
- If DrawStruct^.ItemAction AND oda_DrawEntire>0 Then
- ChangeSelection(DrawStruct) {Draw this line of text in the appropriate colors}
- Else
- If DrawStruct^.ItemAction AND oda_Focus>0 Then
- ChangeFocus(DrawStruct) {The focus changed. Draw / remove the focus box}
- End
- End;
-
-
-
-
- Procedure TOwnerDrawListBox.ChangeFocus(DrawStruct:PDrawItemStruct);
-
- {Change the focus of the ListBox item DrawStruct^.ItemID}
-
- Var
- LineStyle:Integer;
- FGColor:TColorRef;
- NewPen,OldPen:HPen;
- OldBrush,NewBrush:HBrush;
-
- Begin
- If DrawStruct^.ItemState AND ods_Focus>0 Then {This line is getting the focus}
- LineStyle:=ps_Dot
- Else {This line is losing the focus}
- LineStyle:=ps_Solid;
-
- {We now have the line style. Next we need the color. This is determined
- by the line style AND the selected / not selected state}
-
- If (DrawStruct^.ItemID AND $8000 =0) AND (DrawStruct^.ItemState AND ods_Selected>0) Then {This line is selected}
- FGColor:=GetSysColor(Color_Highlight) {Get the standard selected backgtound color}
- Else {This line is de-selected}
- If LineStyle=ps_Dot Then {We are getting the focus, so select the text color}
- FGColor:=GetSysColor(Color_WindowText)
- Else {We are losing the focus, draw in the backgound color}
- FGColor:=GetSysColor(Color_Window);
-
- NewPen:=CreatePen(LineStyle,1,FGColor); {Create the pen...}
- OldPen:=SelectObject(DrawStruct^.HDc,NewPen); {...and select it}
- NewBrush:=GetStockObject(Hollow_Brush); {Get a hollow brush...}
- OldBrush:=SelectObject(DrawStruct^.HDc,NewBrush); {...and select it}
-
- Rectangle(DrawStruct^.HDc,DrawStruct^.rcItem.Left, {Now draw the rectangle}
- DrawStruct^.rcItem.Top,
- DrawStruct^.rcItem.Right,
- DrawStruct^.rcItem.Bottom);
-
- {Finaly, restore the DC's object and delete the pen}
-
- SelectObject(DrawStruct^.HDc,OldPen);
- SelectObject(DrawStruct^.HDc,OldBrush);
-
- DeleteObject(NewPen)
- End;
-
-
-
- Procedure TOwnerDrawListBox.ChangeSelection(DrawStruct:PDrawItemStruct);
-
- {The selection of item DrawStruct has changed}
-
- Var FGColor,BKColor:TColorRef;
- OldFGColor,OldBKColor:TColorRef;
-
- Begin
- {First, get the colors to use}
-
- If (DrawStruct^.ItemState AND ods_Selected>0) Then {This line being selected}
- Begin
- FGColor:=GetSysColor(Color_HighlightText); {Get the standard selected text colors}
- BKColor:=GetSysColor(Color_Highlight)
- End
- Else {This line is being de-selected}
- Begin
- FGColor:=GetSysColor(Color_WindowText); {Get the standard non-selected text colors}
- BKColor:=GetSysColor(Color_Window)
- End;
-
- {Now set the colors for the text}
-
- OldFGColor:=SetTextColor(DrawStruct^.HDc,FGColor);
- OldBKColor:=SetBKColor(DrawStruct^.HDc,BKColor);
-
- DrawBox(DrawStruct); {Draw the text for this line}
-
- SetTextColor(DrawStruct^.HDc,OldFGColor); {Restore the DC's colors}
- SetBKColor(DrawStruct^.HDc,OldBKColor)
- End;
-
-
-
- Procedure TOwnerDrawListBox.DrawBox(DrawStruct:PDrawItemStruct);
-
- {Draw the item for the ListBox line DrawStruct^.ItemID in the
- box DrawStruct^.rcItem. The colors are already set}
-
- Begin
- Self.DrawItem(DrawStruct);
- If DrawStruct^.ItemState AND ods_Focus>0 Then
- ChangeFocus(DrawStruct) {This line has the focus, so draw the focus box}
- End;
-
-
-
- Procedure TOwnerDrawListBox.DrawItem(DrawStruct:PDrawItemStruct);
-
- {Draw the actual item in DrawStruct. Just draw it, everything else is
- taken care of. Must cope with no item selected (item Integer(-1))}
-
- Begin
- {If the ItemData points to a string, draw it. Else, draw an empty string}
-
- If (DrawStruct^.ItemID AND $8000 =0) AND (POINTER(DrawStruct^.ItemData)<>Nil) Then
- ExtTextOut(DrawStruct^.HDc, DrawStruct^.rcItem.Left, DrawStruct^.rcItem.Top,
- eto_Clipped OR eto_Opaque,@DrawStruct^.rcItem,PChar(DrawStruct^.ItemData),
- StrLen(PChar(DrawStruct^.ItemData)),Nil)
- Else
- ExtTextOut(DrawStruct^.HDc, DrawStruct^.rcItem.Left, DrawStruct^.rcItem.Top,
- eto_Clipped OR eto_Opaque,@DrawStruct^.rcItem,' ',1,Nil);
-
- End;
-
-
-
- Begin
- End.
-
-